package com.mstore.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.mstore.pojo.Goods;
import com.mstore.pojo.Tariff;
import com.mstore.service.GoodsService;
import com.mstore.service.TariffService;

public class TariffAction extends BaseAction {

	private static final long serialVersionUID = -939400195201317006L;

	private String amount;

	private String day;

	private File file; // 代表上传文件的File对象

	private String fileContentType; // 上传文件的MIME类型

	private String fileFileName; // 上传文件名

	private long id;

	private long tariffid;

	private int index;

	private int page;

	private int totalSize;

	private TariffService tariffService;

	private List<Tariff> tariffs;

	private Tariff tariff = new Tariff();

	private Goods good = new Goods();

	private GoodsService goodsService;

	private int count;

	private String result;

	/**
	 * 查看资费表
	 */
	public String watchAllTariff() {
		tariffs = tariffService.watchAllTariff();
		return "watchAllTariff";
	}

	public String getList() {
		Tariff tar = new Tariff();
		// 得到短信总条数
		totalSize = tariffService.getCount();

		if (totalSize == 0) {
			totalSize = 1;
		}

		if (totalSize % 14 == 0) {
			page = totalSize / 14;
		}
		else {
			page = totalSize / 14 + 1;
		}

		// 判断如果小于等于0了 就让等于第一页
		if (index <= 0) {
			index = 1;
		}
		// 判断如果大于于等于Count总页数了 就让等于第Count
		if (index >= page) {
			index = page;
		}
		// 分页查询所有短信数据
		tariffs = tariffService.getList(tar, index);
		return "tolist";
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String AjaxSave() throws Exception {

		String lag = "保存失败";
		// 将数据存储在map里，再转换成json类型数据，也可以自己手动构造json类型数据
		Map<String, Object> map = new HashMap<String, Object>();
		/* response.setContentType("text/html;charset=UTF-8"); */
		ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
		String realpath = "/usr/local/nginx/htdoc/wumiImage";

		String newFileName = null;
		// 得到保存上传文件的目录的真实路径
		File dir = new File(realpath);
		if (file != null) {
			// 如果该目录不存在，就创建
			if (!dir.exists()) {
				dir.mkdirs();
			}
			int pos = fileFileName.lastIndexOf(".");
			// 创建文件唯一名称
			String uuid = UUID.randomUUID().toString();
			/* fileFileName+=uuid; */
			fileContentType = fileFileName.substring(pos + 1);// 文件后缀名
			System.out.println(fileContentType);
			newFileName = uuid + "." + fileContentType; // 得到新名称
			InputStream is = new FileInputStream(file);
			OutputStream os = new FileOutputStream(new File(dir, newFileName));
			byte[] buf = new byte[1024];
			int len = -1;
			while ((len = is.read(buf)) != -1) {
				os.write(buf, 0, len);
			}

			is.close();
			os.close();
		}

		String repath = realpath + "\\" + newFileName;
		System.out.println(repath); // 输出路径
		String Name = good.getGoodsName();
		good.setGoodsName(Name);
		good.setGoodsPic(repath); // 插入图片路径

		if (good == null) {
			lag = "保存失败";
		}
		if (goodsService.save(good) == true) {
			good = goodsService.FindById(good.getGoodsId());
			Date createTime = new Date(); // 获取系统当前创建时间
			SimpleDateFormat DateFormate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");// 转换时间格式
			System.out.println(DateFormate.format(createTime));
			tariff.setCreateDate(DateFormate.format(createTime)); // 把转换的时间插入到对象中
			if (amount.equals("") || amount == "") {
				tariff.setValidDays(Integer.parseInt(day));
			}

			if (day.equals("") || day == "") {
				tariff.setAmount(Integer.parseInt(amount));
			}

			tariff.setGoods(good);
			if (tariff == null) {
				lag = "保存失败";
			}
			if (tariffService.save(tariff) == true) {
				lag = "保存成功";
			}
			else {
				lag = "保存失败";
			}

		}
		else {
			lag = "保存失败";
		}
		map.put("lag", lag);
		result = super.mapTojson(map);
		return "save";
	}

	public String toJSP() {
		return "toJSP";
	}

	/**
	 * 根据商品ID查询
	 */
	public String FindGoodId() {
		System.out.println(id);
		good = goodsService.FindById(id);
		if (good == null) {
			// 为空返回列表页面
			return "tologin";
		}
		return "findGoodId";
	}

	public String Delete() {
		tariff = tariffService.FindById(id);
		if (tariff != null) {
			goodsService.Delete(good);
			tariffService.Delete(tariff);
			return "delete";
		}
		return null;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public TariffService getTariffService() {
		return tariffService;
	}

	public void setTariffService(TariffService tariffService) {
		this.tariffService = tariffService;
	}

	public List<Tariff> getTariffs() {
		return tariffs;
	}

	public void setTariffs(List<Tariff> tariffs) {
		this.tariffs = tariffs;
	}

	public Tariff getTariff() {
		return tariff;
	}

	public void setTariff(Tariff tariff) {
		this.tariff = tariff;
	}

	public Goods getGood() {
		return good;
	}

	public void setGood(Goods good) {
		this.good = good;
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public long getTariffid() {
		return tariffid;
	}

	public void setTariffid(long tariffid) {
		this.tariffid = tariffid;
	}

	public String getAmount() {
		return amount;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

}
